import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import Home from "./components/tabbar/HomeContainer.vue"
import My from "./components/tabbar/MyContainer.vue"
import Shopcar from "./components/tabbar/ShopcarContainer.vue"
import Discover from "./components/tabbar/DiscoverContainer.vue"
import newslist from "./components/news/newsList.vue"
import newsInfo from "./components/news/newsInfo.vue"
import photoList from "./components/photos/photoList.vue"
import photosInfo from "./components/photos/photosInfo.vue"
import goodsList from "./components/goods/goodsList.vue"
import goodsInfo from "./components/goods/goodsInfo.vue"
import goodsphot from "./components/goods/goodsphot.vue"
import goodsing from "./components/goods/goodsing.vue"
import login from "./components/login.vue"
var router = new Router({
  routes: [
    { path: "/", redirect: "/Home" },
    { path: "/Home", component: Home },
    { path: "/My", component: My , meta: { requireAuth: true } },
    { path: "/Shopcar", component: Shopcar/* , meta: { requireAuth: true } */ },
    { path: "/Discover", component: Discover },
    { path: "/Home/newslist", component: newslist },
    { path: "/Home/newsInfo/:id", component: newsInfo },
    { path: "/Home/photoList", component: photoList },
    { path: "/Home/photosInfo/:id", component: photosInfo },
    { path: "/Home/goodsList", component: goodsList },
    { path: "/Home/goodsInfo/:id", component: goodsInfo },
    { path: "/Home/goodsphot/:id", component: goodsphot },
    { path: "/Home/goodsing/:id", component: goodsing },
    { path: "/login", component: login },

  ],
  linkActiveClass: "mui-active",
})
router.beforeEach((to, from, next) => {
  console.log(1);
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (localStorage.getItem("token")) {  // 通过vuex state获取当前的token是否存在
      next();
    }
    else {
      next({
        path: '/login',
        /* query: { redirect: to.fullPath } */  // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
  else {
    next();
  }
})
export default router
